package com.mrlv.email.utils;

import com.mrlv.email.entity.EmailMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Map;

/**
 * @author lvshiyu
 * @description: TODO
 * @date 2022年04月11日 11:56
 */
@Component
@ConditionalOnBean(name = "mailSender")
public class EmailUtil {
    /**
     * mailSender 需要在配置中添加 spring.mail.host 才会注入
     */
    @Resource
    private JavaMailSender mailSender;
    @Resource
    private MailProperties mailProperties;

    /**
     * 发送邮件
     * @param emailMessage
     * @return
     */
    public Boolean sendMessage(EmailMessage emailMessage) {
        if (emailMessage.getIsSimple()) {
            return this.sendSimpleMailMessage(emailMessage);
        }
        return this.sendMimeMessage(emailMessage);
    }

    public Boolean sendSimpleMailMessage(EmailMessage emailMessage) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        //谁发的
        simpleMailMessage.setFrom(mailProperties.getUsername());
        //谁要接收
        simpleMailMessage.setTo(emailMessage.getTos());
        //邮件标题
        simpleMailMessage.setSubject(emailMessage.getSubject());
        //邮件内容
        simpleMailMessage.setText(emailMessage.getContent());
        try {
            mailSender.send(simpleMailMessage);
            return true;
//            return JsonReturn.buildSuccess(toEmail.getTos(), "发送普通邮件成功");
        } catch (MailException e) {
            e.printStackTrace();
            return false;
//            return JsonReturn.buildFailure("普通邮件方失败");
        }
    }

    public Boolean sendMimeMessage (EmailMessage emailMessage) {
        //创建一个MINE消息
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper mineHelper = new MimeMessageHelper(mimeMessage, true);
            //谁发
            mineHelper.setFrom(mailProperties.getUsername());
            //谁要接收
            mineHelper.setTo(emailMessage.getTos());
            //邮件主题
            mineHelper.setSubject(emailMessage.getSubject());
            //邮件内容   true 表示带有附件或html
            mineHelper.setText(emailMessage.getContent(), true);
            //构建静态资源, 内嵌文件
            setInlineFiles(mineHelper, emailMessage.getInlineFiles());
            //构建静态资源, 附件
            setAttachment(mineHelper, emailMessage.getAttachments());
            mailSender.send(mimeMessage);
            return true;
//            return JsonReturn.buildSuccess(toEmail.getTos() + toEmail.getContent(), "HTML邮件成功");
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
//            return JsonReturn.buildFailure("HTML邮件失败");
        }
    }

    private void setInlineFiles(MimeMessageHelper mineHelper, Map<String, File> inlineFiles) throws MessagingException {
        if (inlineFiles == null){
            return;
        }
        for (String key : inlineFiles.keySet()) {
            mineHelper.addInline(key, new FileSystemResource(inlineFiles.get(key)));
        }
    }

    private void setAttachment(MimeMessageHelper mineHelper, Map<String, File> attachments) throws MessagingException {
        if (attachments == null){
            return;
        }
        for (String key : attachments.keySet()) {
            mineHelper.addAttachment(key, new FileSystemResource(attachments.get(key)));
        }
    }
}
